/**
 * Created by Administrator on 2017/3/1 0001.
 */
var resource;
var mode;
var pixiStageW=740;
var pixiStageH=1136;
var pixiStage=new PIXI.Stage(0xFF0000);
var ratio;
var canvas;
var PixiMain={
    create:function () {
        ratio=window.devicePixelRatio;

        var pixi_renderer=new PIXI.CanvasRenderer(pixiStageW,pixiStageH,{antialias:true,transparent:true, roundPixels: true,resolution:ratio});
        canvas=pixi_renderer.view;
        var div_pixi=document.getElementById('div_2d');
        div_pixi.appendChild(pixi_renderer.view);
        // div_pixi.width=pixiStageW*ratio+'px';
        // div_pixi.height=pixiStageH*ratio+'px';
        // div_pixi.style.width=pixiStageW+'px';
        // div_pixi.style.height=pixiStageH+'px';

        var preload=Preload.create();
        preload.init(pixiStage,pixiStageW,pixiStageH);
        var con_whole=new PIXI.Container();
        pixiStage.addChild(con_whole);

        document.addEventListener('Main_LoadComplete',gameStart);
        function gameStart(e) {
            resource=e.detail.resources;
            var game=Game.create();
            pixiStage.addChildAt(game.ui,0);
            pixi_renderer.render(pixiStage);
            // var dataURL = pixi_renderer.view.toDataURL('image/png');
            // document.getElementById('img').src=dataURL;
            // console.log(dataURL);
        }

        resize();
        window.onresize = resizeHandlerPIXI;
        function resizeHandlerPIXI() {
            setTimeout(function () {
                resize();
            },200)
        };
        function resize()
        {
            canvas.width = window.innerWidth * ratio;
            canvas.height = window.innerHeight * ratio;
            canvas.style.width = window.innerWidth + 'px';
            canvas.style.height = window.innerHeight + 'px';
            pixi_renderer.resize(window.innerWidth*ratio, window.innerHeight*ratio);
            mode='noBorder';
            switch (mode) {
                case 'exactFit':
                    pixiStage.scale.x = window.innerWidth / pixiStageW;
                    pixiStage.scale.y = window.innerHeight / pixiStageH;
                    break;
                case 'noBorder':
                    pixiStage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerWidth / pixiStageW)*ratio : (window.innerHeight / pixiStageH)*ratio;
                    pixiStage.scale.y = pixiStage.scale.x;
                    break;
                case 'noScale':
                    pixiStage.scale.x = 1;
                    pixiStage.scale.y = 1;
                    break;
                case 'showAll':
                    pixiStage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerHeight / pixiStageH) : (window.innerWidth / pixiStageW);
                    pixiStage.scale.y = pixiStage.scale.x;
                    break;
            }
            pixiStage.x = (window.innerWidth*ratio - pixiStageW * pixiStage.scale.x) * .5;
            pixiStage.y = (window.innerHeight*ratio - pixiStageH * pixiStage.scale.y) * .5;


            document.getElementById('img_long').style.width= pixiStage.scale.x/ratio*740+'px';
            document.getElementById('img_long').style.height= pixiStage.scale.y/ratio*1883+'px';
            document.getElementById('img_long').style.left= (window.innerWidth-document.getElementById('img_long').style.width)/2+'px';
            // document.getElementById('img_long').style.left= pixiStage.scale.x*0+pixiStage.x+'px';
            // document.getElementById('img_long').style.top= pixiStage.scale.y*(0)+pixiStage.y+'px';
            document.getElementById('img_qr').style.width= pixiStage.scale.x/ratio*740+'px';
            document.getElementById('img_qr').style.height= pixiStage.scale.y/ratio*1883+'px';
            document.getElementById('img_qr').style.left= (window.innerWidth-document.getElementById('img_qr').style.width)/2+'px';
            // document.getElementById('img_qr').style.left= pixiStage.scale.x*0+pixiStage.x+'px';
            // document.getElementById('img_qr').style.top= pixiStage.scale.y*(0)+pixiStage.y+'px';

            document.getElementById('img_back').style.width= pixiStage.scale.x/ratio*65+'px';
            document.getElementById('img_back').style.height= pixiStage.scale.y/ratio*65+'px';
            document.getElementById('img_back').style.left= pixiStage.scale.x/ratio*625+pixiStage.x+'px';
            document.getElementById('img_back').style.top= pixiStage.scale.y/ratio*50+pixiStage.y+'px';
        }
        // var globalID;
        // globalID=
        requestAnimationFrame(animate);
        // cancelAnimationFrame(globalID);
        function animate() {
            requestAnimationFrame(animate);
            pixi_renderer.render(pixiStage);
        }
    }
}